# encoding:utf-8
import os
import json
import xml.etree.ElementTree as ET
from lxml import etree


def generate_fake_reports():
    report = {}
    BL_Dict = {"age":49, "body_of_case": "左乳腺",
               "desc_txt":"乳腺癌新辅助化疗后标本，镜下可见乳腺纤维间质、导管、小叶增生，局部见大量泡沫样细胞增生，其中可见少量残留的肿瘤细胞，周围淋巴细胞包饶",
               "diag_txt": "1、（左乳）乳腺癌新辅助化疗后标本，可见少量肿瘤细胞残留，根据Miller-Payne分级系统，符合3级。\n 2、（左乳）乳头、皮肤切缘、乳腺四周切缘及底部切缘未见癌。\n 3、（左腋窝淋巴结）检出淋巴结（0/11）未见癌。"}

    FS_Dict = {"age": 49, "body_of_case": "双侧乳腺钼靶摄片检查/4片(数字化)",
               "desc_txt": "双侧乳腺多量腺体型，腺体呈斑片、条索状影分布，密度不均匀，边缘模糊。左乳上象限见一巨大肿块影，大小约62.3mm×77.6mm，边界欠清，边缘欠光滑，密度较高，其内可见数个点状钙化，皮肤增厚，皮下脂肪间隙浑浊，乳头无明显内陷。右乳未见明确肿块及异常钙化影，皮肤无增厚，乳头无内陷。双侧腋下见多个淋巴结影，左腋下淋巴结增大、密度增高，最大者长径约30.2mm；右腋下淋巴结密度不均匀。",
               "diag_txt": "1.左乳肿块，考虑乳腺癌可能性大。（钼靶BI-RADS 5类）\n 2.左腋下多发肿大淋巴结。\n 3.右侧乳腺未见明确肿瘤X线征。（钼靶BI-RADS 1类）"}

    report["BL_info"] = BL_Dict
    report["FS_info"] = FS_Dict

    json_file = "fake_report.json"

    with open(json_file, "w", encoding="utf-8") as outfile:
        outfile.write(json.dumps(report, ensure_ascii=False))


def read_fake_json():
    json_file = "fake_report.json"
    report = json.load(open(json_file, 'r', encoding='utf-8'))
    return report


class GetXML:
    def Read(self,xmlfilename):
        '将XML文件解析为树,并且得到根节点'
        #tree = ET.parse(xmlfilename)
        tree = etree.parse(xmlfilename)
        self.root = tree.getroot()
        return self.root

    def Iter(self):
        '递归迭代xml文件中所有节点（包含子节点，以及子节点的子节点）'
        return self.root.iter()

    def FindAll(self,tag):
        '查找节点为tag的所有直接子元素'
        #直接子元素的意思：只会查找当前节点的子节点那一级目录
        return self.root.findall(tag)

    def Find(self,tag):
        '查找第一个节点为tag的直接子元素'
        return self.root.find(tag)



def parse_case_xml(xml_file):
    xml_instance = GetXML()
    xml_instance.Read(xml_file)
    multimedia_info_node = xml_instance.Find('XmlData').find('multimedia_info')
    dept_type_node = multimedia_info_node.find('dept_type')
    image_count_node = multimedia_info_node.find('image_count')
    report_desc_txt_node = multimedia_info_node.find('report_desc_txt')
    report_diag_txt_node = multimedia_info_node.find('report_diag_txt')

    register_info_node = xml_instance.Find('XmlData').find('register_info')
    patient_age_node = register_info_node.find('patient_age')
    body_of_case_node = register_info_node.find('body_of_case')

    return {"modality": dept_type_node.text,
            "xml_name": str(xml_file),
            "age": patient_age_node.text,
            "body_of_case": body_of_case_node.text,
            "image_count": image_count_node.text,
            "desc_text": report_desc_txt_node.text,
            "diag_text": report_diag_txt_node.text}



def test_xml():
    file = "E://medical//breast//乳腺癌//001091461200//572619-王小琴-20190430367146//diagnosis.txt"
    xml_instance = GetXML()
    xml_instance.Read(file)

    multimedia_info_node = xml_instance.Find('XmlData').find('multimedia_info')
    print(multimedia_info_node.tag)
    print(multimedia_info_node.text)
    print(multimedia_info_node.attrib)
    print("---------------------------")

    dept_type_node = multimedia_info_node.find('dept_type')
    print("dept_type_node: ", dept_type_node.text)
    print("---------------------------")

    image_count_node = multimedia_info_node.find('image_count')
    print("image_count_node: ", image_count_node.text)
    print("---------------------------")

    report_desc_txt_node = multimedia_info_node.find('report_desc_txt')
    print("report_desc_txt_node: ", report_desc_txt_node.text)
    print("---------------------------")

    report_diag_txt_node = multimedia_info_node.find('report_diag_txt')
    print("report_diag_txt_node: ", report_diag_txt_node.text)
    print("******************************************")

    register_info_node = xml_instance.Find('XmlData').find('register_info')
    print(register_info_node.tag)
    print(register_info_node.text)
    print(register_info_node.attrib)
    print("---------------------------")
    patient_age_node = register_info_node.find('patient_age')
    print("patient_age_node: ", patient_age_node.text)
    print("---------------------------")
    body_of_case_node = register_info_node.find('body_of_case')
    print("body_of_case_node: ", body_of_case_node.text)
    print("---------------------------")





#test_xml()

